<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>放大镜</title>
    <style>
        body {
            margin: 0;
            padding: 0;
        }

        #view {
            background-image: url(../img/0.jpg);
            height: 500px;
            width: 800px;
        }

        #yellowDiv {
            background-color: rgba(255, 239, 167, 0.2);
            position: absolute;
            display: none;
            width: 120px;
            height: 70px;
            transform: all 0.2s;
            top: 0;
            left: 0;
        }
    </style>
    <script src="../lib/jquery-3.5.1.js"></script>
</head>

<body>
    <div id="yellowDiv"></div>
    <div> <img id="img" src="../img/0.jpg" alt="英雄联盟的图片" width="25%"></div>
    <div id="view"> </div>

    <script>
        $(function (e) {
            $("#img").mousemove(function (e) {
                $("#yellowDiv").show();
                let x = e.pageX - 50;
                let y = e.pageY - 35;
                if (x >= 280) {
                    x = 280;
                }
                if (y >= 55) {
                    y = 55;
                }
                if (y <= 0) {
                    y = 0;
                }
                if (x <= 0) {
                    x = 0;
                }
                let yellowOffSet = $("#yellowDiv").offset().left;
                console.log(yellowOffSet);
                $("#view").css("backgroundPositionX", -x * 4);
                $("#view").css("backgroundPositionY", -y * 4);

                // $("#yellowDiv").css({ "left": x, "top": y });
                $("#yellowDiv").mousemove(function (e) {
                    $("#yellowDiv").css({ "left": x - 60, "top": y - 35 });
                })
            })


        })

    </script>
</body>

</html>